<template>
  <div class="music-list" teleport="body">
    <van-popup v-model:show="showPopup" position="bottom" :style="popupStyle" style="background: var(--box-color)">
      <div class="music-list_btn-group">
        <div :class="[swipeActive === 0 ? '_active' : '']" @click="handleSwitchSwipe(0)">历史播放</div>
        <div :class="[swipeActive === 1 ? '_active' : '']" @click="handleSwitchSwipe(1)">推荐音乐</div>
      </div>
      <van-swipe class="my-swipe" :show-indicators="false" @change="handleChangeSwipe" :loop="false" ref="swipeRef">
        <van-swipe-item
          ><music-item
            v-for="(item, index) in musicHistoryList"
            :music-detail="item"
            :key="index"
            @play-music="handlePlayMusic"
        /></van-swipe-item>
        <van-swipe-item
          ><music-item
            v-for="(item, index) in recommandMusicList"
            :music-detail="item"
            :key="index"
            @play-music="handlePlayMusic"
        /></van-swipe-item>
      </van-swipe>
    </van-popup>
  </div>
</template>

<script setup lang="ts">
import type { SwipeInstance } from "vant";
import { ref, computed, toRefs, type CSSProperties } from "vue";
import musicItem from "./childItem/musicItem.vue";
import usePlayerStore from "@/store/playerStore";
import mitter from "@/utils/mitt";
const { recommandMusicList, musicHistoryList } = toRefs(usePlayerStore());
const swipeRef = ref<SwipeInstance>();
const showPopup = ref(false),
  swipeActive = ref(0);

const popupStyle = computed(() => ({ height: "60%", borderTopLeftRadius: "30px", overflow: "unset" } as CSSProperties));

const handlePlayMusic = (id: number) => {
  mitter.emit("playMusic", id);
  showPopup.value = false;
};

const handleChangeSwipe = (index: number) => {
  swipeActive.value = index;
};

const handleSwitchSwipe = (index: number) => {
  swipeRef.value?.swipeTo(index);
};

const openPopup = () => {
  showPopup.value = true;
};
defineExpose({
  openPopup,
});
</script>

<style scoped lang="scss">
.music-list {
  color: var(--font-color);
  &_btn-group {
    position: absolute;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 100%;
    div {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 40px;
      padding: 0 16px;
      color: hsla(0, 0%, 100%, 0.9);
      background: #c0e3d8;
      border-radius: 8px;
      font-size: 14px;
      transition: all 0.3s ease-in-out;
      font-weight: 600;
    }
    ._active {
      box-shadow: -1px 4px 10px rgb(0 0 0 / 20%);
      color: #fff;
      background: #ccf2f4;
    }
  }
  .my-swipe {
    margin-top: 25px;
    height: calc(60vh);
    overflow-y: auto;
  }
}
</style>
